package com.meilai.project.controller.mini;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.controller.web.business.contract.dto.ContractApplySaveReq;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.dto.business.contract.ContractApplyCheckDTO;
import com.meilai.project.dto.business.contract.ContractApplyListDTO;
import com.meilai.project.entity.personnel.org.Department;
import com.meilai.project.service.business.contract.ContractApplyService;
import com.meilai.project.util.ThreadLocalUserUtil;
import com.meilai.project.vo.business.contract.ContractApplyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("小程序-登录")
@RestController
@RequestMapping("api/mini/contractApply")
public class MiniContractApplyController {

    @Autowired
    private ContractApplyService service;

    @GetMapping("/queryPartyANameByCustomerId")
    @ApiOperation("根据客户ID获取甲方名称待选项")
    public ApiResult<List<String>> queryPartyANameByCustomerId(@RequestParam("customer_id") Long customer_id) {
        return null;
    }

    @GetMapping("/queryChildCompany")
    @ApiOperation("查询子公司待选项")
    public ApiResult<List<Department>> queryChildCompany() {
        return null;
    }

    @GetMapping("/querySpecByMediaTypeId")
    @ApiOperation("根据媒体类型ID查询规格")
    public ApiResult<List<String>> querySpecByMediaTypeId(@RequestParam("media_type_id") Long media_type_id) {
        return null;
    }

//    @PostMapping("/begin")
//    @ApiOperation("开启流程")
//    public ApiResult<Boolean> begin(@RequestBody ContractApplySaveReq req) {
//        return null;
//    }
//
//    @PostMapping("/save")
//    @ApiOperation("保存")
//    public ApiResult<Boolean> save(@RequestBody ContractApplySaveReq req) {
//        return null;
//    }
//
//    @GetMapping("/detail")
//    @ApiOperation("详情")
//    public ApiResult<ContractApplyVO> detail(@RequestParam("id") Long id) {
//        return null;
//    }


    @GetMapping("list-latest-by-customer-id")
    @ApiOperation(value = "根据customerId查合同列表")
    public ApiResult<List<ContractApplyVO>> listByCustomerId(@ApiParam("customerId") Long customerId) {
        List<ContractApplyVO> regularApplyVOPage = service.listLatestByCustomerId(customerId);
        return ApiResult.ok(regularApplyVOPage);
    }

//    @Autowired
//    private BillNumberService billNumberService;
//
//    @Autowired
//    private IntentionSchemeService intentionSchemeService;
//
//	/*@PostMapping("generate")
//	@ApiOperation(value = "多个方案生成合同")
//	public ApiResult<Long> generateContract(@ApiParam("json") @RequestBody ContractGenerateDTO req) {
//		return ApiResult.ok(service.generateContract(req));
//	}*/
//
//	/*@GetMapping("generate_contract_num")
//	@ApiOperation(value = "生成合同编号", notes = "生成合同编号")
//	public ApiResult<ContractAttachment> generate_attachment() {
//		String contractNum = "XSHT" + DateUtil.format(new Date(), "yyyy");
//		Integer currentNum = contractNumService.getCurrentNum(1);
//		String numStr = StrUtil.fillBefore(currentNum.toString(), '0', 4);
//		return ApiResult.ok(contractNum + numStr);
//	}*/
//
//    @GetMapping("list-not-related-intention-scheme-contract")
//    @ApiOperation(value = "查询还未关联锁位的合同")
//    public ApiResult<List<NotCompleteSaleContractVO>> listNotRelatedIntentionSchemeContract(
//            @ApiParam("userId") @RequestParam(value = "userId", required = false) Long userId) {
//        return ApiResult.ok(service.listNotComplete(userId));
//    }
//
    @GetMapping("part_a_history")
    @ApiOperation(value = "甲方输入历史(根据客户ID查询)")
    public ApiResult<List<String>> partAHistory(@ApiParam("客户ID") @RequestParam("customerId") Long customerId) {
        List<String> partAHistoryList = service.partAHistory(customerId);
        return ApiResult.ok(partAHistoryList);
    }
//
//	/*@GetMapping("list-amend-contract")
//	@ApiOperation(value = "查询补充协议合同")
//	public ApiResult<AmendContractListVO> listByRootContractId(Long root_id) {
//		List<ContractApplyVO> result = service.listByRootContractId(root_id);
//		return ApiResult.ok(result);
//	}*/
//
//
//    @GetMapping("list_by_customer_id")
//    @ApiOperation(value = "根据customerId查合同列表，变更销售单时新增")
//    public ApiResult<List<ContractApplyVO>> list_by_customer_id(@ApiParam("customerId") Long customerId) {
//        List<ContractApplyVO> regularApplyVOPage = service.list_by_customer_id(customerId);
//        return ApiResult.ok(regularApplyVOPage);
//    }
//
//    @GetMapping("list-latest-for-market-by-customer-id")
//    @ApiOperation(value = "根据customerId查合同列表(for市场单)")
//    public ApiResult<List<ContractApplyVO>> listForMarketByCustomerId(
//            @ApiParam("customerId") Long customerId,
//            @ApiParam("mustIncludeContractId") Long mustIncludeContractId
//    ) {
//        List<ContractApplyVO> regularApplyVOPage = service.listForMarketByCustomerId(customerId, mustIncludeContractId);
//        return ApiResult.ok(regularApplyVOPage);
//    }
//
//    @GetMapping("list-latest-for-ad-make-by-dept-id")
//    @ApiOperation(value = "根据deptId查合同列表(for制作单)")
//    public ApiResult<List<ContractApplyForADMakeVO>> listForADMakeByDeptId(
//            @ApiParam("deptId") Long deptId,
//            @ApiParam("mustIncludeContractId") Long mustIncludeContractId
//    ) {
//        List<ContractApplyForADMakeVO> regularApplyVOPage = service.listForADMakeByDeptId(deptId, mustIncludeContractId);
//        return ApiResult.ok(regularApplyVOPage);
//    }
//
//    @GetMapping("list-by-root-id")
//    @ApiOperation(value = "列表分页")
//    public ApiResult<Page<ContractApplyVO>> listByRootId(Long rootContractId) {
//        List<ContractApplyVO> regularApplyVOPage = service.listByRootContractId(rootContractId);
//        return ApiResult.ok(regularApplyVOPage);
//    }
//
    @GetMapping("list")
    @ApiOperation(value = "列表分页")
    public ApiResult<Page<ContractApplyVO>> list(@ApiParam("form") ContractApplyListDTO req) {
        if(req.getIsMy()) req.setUserId(ThreadLocalUserUtil.get().getId());
        Page<ContractApplyVO> regularApplyVOPage = service.selectPageList(req);
        return ApiResult.ok(regularApplyVOPage);
    }
//
//    @GetMapping("list-latest")
//    @ApiOperation(value = "查询最新合同-列表分页")
//    public ApiResult<Page<ContractApplyVO>> listLatest(@ApiParam("form") ContractApplyLatestListDTO req) {
//        Page<ContractApplyVO> regularApplyVOPage = service.selectLatestPageList(req);
//        return ApiResult.ok(regularApplyVOPage);
//    }
//
//	/*@GetMapping("list-by-amend-type")
//	@ApiOperation(value = "按类型查询补充协议列表分页")
//	public ApiResult<Page<ContractApplyVO>> listByAmendType(@ApiParam("form") ContractApplyAmendListDTO req) {
//		Page<ContractApplyVO> regularApplyVOPage = service.selectPageAmendList(req);
//		return ApiResult.ok(regularApplyVOPage);
//	}*/
//
//
    @GetMapping("detail/{id}")
    @ApiOperation(value = "单据明细")
    public ApiResult<ContractApplyVO> detail(@ApiParam("id") @PathVariable("id") Long id) {
        return ApiResult.ok(service.getOneById(id));
    }
//
//    @GetMapping("get-by-intention-scheme-id")
//    @ApiOperation(value = "单据明细(根据锁位id查)")
//    public ApiResult<ContractApplyVO> getByIntentionSchemeId(@ApiParam("intentionSchemeId") Long intentionSchemeId) {
//        IntentionSchemeVO schemeVO = intentionSchemeService.getOneById(intentionSchemeId);
//        return ApiResult.ok(service.getOneById(schemeVO.getRelated_contract_id()));
//    }
//
//	/*@GetMapping("detail/date_range")
//	@ApiOperation(value = "根据合同id和daterange_id查单据明细")
//	public ApiResult<ContractApplyVO> detailDateRange(
//			@ApiParam("id") @RequestParam("id") Long id,
//			@ApiParam("id") @RequestParam("daterange_id") Long daterange_id
//	) {
//		return ApiResult.ok(service.getOneDateRangeById(id, daterange_id));
//	}*/
//
    @PostMapping("save")
    @ApiOperation("保存")
    public ApiResult<Boolean> save(@ApiParam("json") @RequestBody ContractApplySaveReq req) {
        try {
            ApiResult<Boolean> apiResult = service.save(req);
            if (null == apiResult) {
                return ApiResult.ok("新增成功");
            }
            return apiResult;
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PostMapping("begin")
    @ApiOperation("提交")
    public ApiResult<Boolean> begin(@ApiParam("json") @RequestBody ContractApplySaveReq req) {
        try {
            ApiResult<Boolean> apiResult = service.begin(req);
            if (null == apiResult) {
                return ApiResult.ok("提交成功");
            }
            return apiResult;
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("pass")
    @ApiOperation("通过")
    public ApiResult<Boolean> pass(@ApiParam("json") @RequestBody ContractApplyCheckDTO req) {
        try {
            if (service.pass(req)) {
                return ApiResult.ok("通过成功");
            } else {
                return ApiResult.err("通过失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("return_back")
    @ApiOperation("驳回")
    public ApiResult<Boolean> returnBack(@ApiParam("json") @RequestBody ContractApplyCheckDTO req) {
        try {
            if (service.returnBack(req)) {
                return ApiResult.ok("驳回成功");
            } else {
                return ApiResult.err("驳回失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @GetMapping("delete")
    @ApiOperation(value = "逻辑删除")
    public ApiResult<Boolean> delete(@RequestParam("id") @ApiParam("id") Long id) {
        try {
            if (service.delete(id)) {
                return ApiResult.ok("删除成功");
            } else {
                return ApiResult.err("删除失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @GetMapping("actual_delete")
    @ApiOperation(value = "物理删除")
    public ApiResult<Boolean> actualDelete(@RequestParam("id") @ApiParam("id") Long id) {
        try {
            if (service.actualDelete(id)) {
                return ApiResult.ok("彻底删除成功");
            } else {
                return ApiResult.err("彻底删除失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }
}
